package org.jeecg.modules.meta.service.impl;


import cn.hutool.core.date.DateUtil;
import org.jeecg.desen.cretab.service.DesensDatabaseCreateService;
import org.jeecg.modules.meta.service.MetaSynDBOperService;
import org.jeecg.modules.meta.service.MetaSynService;
import org.jeecg.modules.pubmgr.entity.Paramcode;
import org.jeecg.modules.pubmgr.entity.Parameter;
import org.jeecg.modules.pubmgr.service.IParamcodeService;
import org.jeecg.modules.pubmgr.service.IParameterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MetaSynServiceImpl implements MetaSynService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private final static String PARAM_PARAMCODE = "is_run";
    private final static String PARAM_PARAMNAME = "des_status";

    @Resource(name = "metaSynDBOperAllServiceImpl")
    MetaSynDBOperService metaSynDBOperAllService;

    @Resource(name = "metaSynDBOperTimeServiceImpl")
    MetaSynDBOperService metaSynDBOperTimeService;

    @Autowired
    IParamcodeService iParamcodeService;

    @Autowired
    IParameterService parameterService;

    @Autowired
    DesensDatabaseCreateService desensDatabaseCreateService;

    @Override
    public boolean synMetaAll() {
        Date date = new Date();
        log.info("元数据同步All:日期" + DateUtil.formatDateTime(date));
        return metaSynDBOperAllService.synMeta(date);
    }

    @Override
    public boolean synMetaTime(Date date) {
        log.info("元数据同步当天:日期" + DateUtil.formatDateTime(date));

        boolean blean = metaSynDBOperTimeService.synMeta(date);
        if (true == blean) {
            return desensDatabaseCreateService.createHiveTableByStatus(date);
        }
        return blean;
    }

    /**
     * 判断是否正在同步，从关系型数据获取标识符 分布式后需要从redis等内存数据库查找 TODO 分布式 RPC
     * 暂时不考虑正在脱敏跑批的作业 如果正在作业返回系统正在作业
     *
     * @return
     */
    @Override
    public boolean isSyn() {
        Paramcode paramcode = iParamcodeService.getParamPreByCode(PARAM_PARAMNAME, PARAM_PARAMCODE);
        if (null == paramcode) {
            return false;
        }

        return "Y" == paramcode.getFdParameterprice();
    }

    @Override
    public void startSyn(String desc) {
        log.info("starySyn:" + desc);
        updateSynMeateAll("Y", desc);
    }

    @Override
    public void endSyn(String desc) {
        log.info("endSyn:" + desc);
        updateSynMeateAll("N", desc);
    }

    private void updateSynMeateAll(String val, String desc) {
        int nt = iParamcodeService.updateParamCodeByNameCode(PARAM_PARAMNAME, PARAM_PARAMCODE, val, desc);
        if (nt == -1) {//是否需要插入数据
            initParmDes();
        }
    }

    private void initParmDes() {
        Parameter parameter = new Parameter();
        parameter.setFdParamname(PARAM_PARAMNAME);
        parameter.setFdParamdesc("后台同步功能");

        List<Paramcode> list = new ArrayList<>();

        Paramcode paramcode = new Paramcode();
        paramcode.setFdParamcode(PARAM_PARAMCODE);
        paramcode.setFdParamcodename("应用同步运行标识");
        paramcode.setFdParameterprice("N");
        paramcode.setFdIddefault("N");
        paramcode.setFdParametertype("String");
        paramcode.setFdParamcodedesc("是否正在同步");
        paramcode.setCreateTime(new Date());
        list.add(paramcode);

        parameterService.saveMain(parameter, list);
    }
}
